package com.project2.service;


import com.project2.bean.po.MarketStorage;
import com.project2.mapper.MarketStorageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author : bunnyupup
 * @description :
 * @date : Created in 2022/8/15 16:42
 */
@Service
public class WxStorageServiceImpl implements WxStorageService {
    @Autowired
    MarketStorageMapper marketStorageMapper;

    @Override
    public MarketStorage addpic(MultipartFile myFile) {
        long size = myFile.getSize();//图片大小
        String originalFilename = myFile.getOriginalFilename();//图片name
        String[] split = originalFilename.split("\\.");
        String random = UUID.randomUUID().toString().replaceAll("-", "");
        String key = random + "." + split[split.length - 1];//key
        Date date = new Date();//当前时间
        String contentType = myFile.getContentType();//type
        String url =  "http://localhost:8083/"+key;//ur;

        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(key);
        marketStorage.setName(originalFilename);
        marketStorage.setType(contentType);
        marketStorage.setSize((int) size);
        marketStorage.setAddTime(date);
        marketStorage.setUpdateTime(date);
        marketStorage.setId(null);
        marketStorage.setDeleted(false);
        marketStorage.setUrl(url);

        marketStorageMapper.insertSelective(marketStorage);


        File file = new File("D://img/", key);
        try {
            myFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return marketStorage;
    }
}
